package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.system.domain.SysNotice;
import com.muyu.system.domain.model.*;
import com.muyu.system.mapper.SysNoticeMapper;
import com.muyu.system.service.SysNoticeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author muyu
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    /**
     * 通知公告份也查询
     * @param sysNoticePageQueryModel 分页查询模型
     * @return 分页结果
     */
    @Override
    public PageQueryModel<SysNoticeListModel> pageQuery(SysNoticePageQueryModel sysNoticePageQueryModel) {
        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(sysNoticePageQueryModel.getNoticeTitle()),SysNotice::getNoticeTitle, sysNoticePageQueryModel.getNoticeTitle());
        queryWrapper.eq(StringUtils.isNotEmpty(sysNoticePageQueryModel.getNoticeType()),SysNotice::getNoticeType, sysNoticePageQueryModel.getNoticeType());
        queryWrapper.like(StringUtils.isNotEmpty(sysNoticePageQueryModel.getCreateBy()),SysNotice::getCreateBy, sysNoticePageQueryModel.getCreateBy());
        Page<SysNotice> page = this.page(sysNoticePageQueryModel.buildPage(), queryWrapper);
        List<SysNoticeListModel> noticeListModels = page.getRecords().stream()
                .map(SysNoticeListModel::listInfoBuildToModel)
                .toList();
        Page<SysNoticeListModel> modelPage = new Page<>(page.getCurrent(), page.getSize());
        modelPage.setRecords(noticeListModels);
        modelPage.setTotal(page.getTotal());
        modelPage.setOrders(page.orders());
        return PageQueryModel.of(modelPage);
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     *
     * @return 公告信息
     */
    @Override
    public SysNoticeDetailModel selectNoticeById (Long noticeId) {
        SysNotice sysNotice = this.getById(noticeId);
        return SysNoticeDetailModel.infoBuildToDetailModel(sysNotice);
    }

    /**
     * 新增公告
     *
     * @param sysNoticeAddModel 通知公告添加模型
     */
    @Override
    public void insertNotice (SysNoticeAddModel sysNoticeAddModel) {
        this.save(SysNotice.addModelBuildToInfo(sysNoticeAddModel));
    }

    /**
     * 修改公告
     *
     * @param id 通知公告id
     * @param sysNoticeUpdModel 公告信息修改模型
     */
    @Override
    public void updateNotice (Long id, SysNoticeUpdModel sysNoticeUpdModel) {
        this.updateById(SysNotice.updReqBuildToInfo(id, sysNoticeUpdModel));
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     */
    @Override
    public void deleteNoticeById (Long noticeId) {
        this.removeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     */
    @Override
    public void deleteNoticeByIds (List<Long> noticeIds) {
        this.removeByIds(noticeIds);
    }

}
